<?php
include_once(dirname(__FILE__)."/../inc/common.php");
$method = isset($_GET["method"])?$_GET["method"]:"";

$flowClass = new tally_flow();

if($method=="listQry") {
    // 所选月收入与支出
    $year = isset($_POST["year"])?$_POST["year"]:null;
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $startTime = strtotime($year."-01-01 00:00:00");
    $endTime = strtotime(($year+1)."-01-01 00:00:00")-1;

    $thisYearIn = $flowClass->getCountByTimeRangeGroupByType($userId,
        tally_flow::$INOUT_INCOMME, $startTime, $endTime, $tagIds, $categoryId);
    $thisYearOut = $flowClass->getCountByTimeRangeGroupByType($userId,
        tally_flow::$INOUT_EXPENSE, $startTime, $endTime, $tagIds, $categoryId);
    $inByMonth = array();
    $outByMonth = array();
    foreach($thisYearIn as $row){
        $flowType = $row["flowType"];
        $inByMonth[$flowType] = array(
            "flowTypeId" => $row["flowType"],
            "flowTypeName" => $row["name"],
            "money" => array()
        );
        for($month = 1;$month<=12;$month++){
            $nextMonthYear = $year;
            $nextMonth = $month+1;
            if($nextMonth>12){
                $nextMonth = 1;
                $nextMonthYear++;
            }
            $startTime = $year."-".$month."-01 00:00:00";
            $endTime = date("Y-m-d H:i:s", strtotime($nextMonthYear."-".$nextMonth."-01 00:00:00")-24*60*60);
            $in = $flowClass->inCountByUserId($userId, 0, $row["flowType"], $startTime, $endTime, $tagIds, "", "", "", "", "");
            $inByMonth[$flowType]["money"][$month] = $in?$in:0;
        }
    }
    foreach($thisYearOut as $row){
        $flowType = $row["flowType"];
        if($categoryId && $categoryId!=$flowType){
            continue;
        }
        $outByMonth[$flowType] = array(
            "flowTypeId" => $row["flowType"],
            "flowTypeName" => $row["name"],
            "money" => array()
        );
        for($month = 1;$month<=12;$month++){
            $nextMonthYear = $year;
            $nextMonth = $month+1;
            if($nextMonth>12){
                $nextMonth = 1;
                $nextMonthYear++;
            }
            $startTime = $year."-".$month."-01 00:00:00";
            $endTime = date("Y-m-d H:i:s", strtotime($nextMonthYear."-".$nextMonth."-01 00:00:00")-24*60*60);
            $out = $flowClass->outCountByUserId($userId, 0, $row["flowType"], $startTime, $endTime, $tagIds, "", "", "", "", "");
            $outByMonth[$flowType]["money"][$month] = $out?$out:0;
        }
    }
    ?>
    <!--begin:数据列表-------------------------------------------------------------------------------------------------->
    <table id="listtb" class="listtable" cellpadding="0" cellspacing="1">
        <tr>
            <th width="1%">&nbsp;</th>
            <th width="12%">类别</th>
            <th width="6%">一月</th>
            <th width="6%">二月</th>
            <th width="6%">三月</th>
            <th width="6%">四月</th>
            <th width="6%">五月</th>
            <th width="6%">六月</th>
            <th width="6%">七月</th>
            <th width="6%">八月</th>
            <th width="6%">九月</th>
            <th width="6%">十月</th>
            <th width="6%">十一月</th>
            <th width="6%">十二月</th>
            <th width="6%">全年</th>
        </tr>


        <tr>
            <td></td>
            <td colspan="14" style="text-align:left;font-weight:bold;">收入</td>
        </tr>

        <?php
        $inByMonthSum = array();
        $allByMonthSum = array();
        foreach($inByMonth as $row){
        ?>
        <TR>
            <TD></TD>
            <TD class="char">
                <a href="javascript:fGoto();" onclick="ReportDetail.open(<?php echo $row["flowTypeId"];?>);"><?php echo $row["flowTypeName"];?></a>
            </TD>
            <?php
            foreach($row["money"] as $month=>$money){
                if(!isset($inByMonthSum[$month])){
                    $inByMonthSum[$month] = 0;
                }
                $inByMonthSum[$month] += $money;
            ?>
            <TD class="number"><?php echo number_format($money, 2, ".", "");?></TD>
            <?php
            }
            ?>
        </TR>
        <?php
        }
        ?>


        <TR  id="content">
            <TD></TD>
            <TD class="char">收入小计</TD>
            <?php
            $inSum = 0;
            foreach($inByMonthSum as $month=>$money){
                if(!isset($allByMonthSum[$month])){
                    $allByMonthSum[$month] = 0;
                }
                $allByMonthSum[$month] += $money;
                $inSum += $money;
                ?>
                <TD class="number"><?php echo number_format($money, 2, ".", "");?></TD>
                <?php
            }
            ?>
            <TD class="number" title="<?php echo number_format($inSum, 2, ".", "");?>"><?php echo number_format($inSum, 2, ".", "");?></TD>
        </TR>


        <tr>
            <td></td>
            <td colspan="14" style="text-align:left;font-weight:bold;padding-left:5px;">支出</td>
        </tr>



        <?php
        $outByMonthSum = array();
        foreach($outByMonth as $row){
            ?>
            <TR>
                <TD></TD>
                <TD class="char">
                    <a href="javascript:fGoto();" onclick="ReportDetail.open(<?php echo $row["flowTypeId"];?>);"><?php echo $row["flowTypeName"];?></a>
                </TD>
                <?php
                foreach($row["money"] as $month=>$money){
                    if(!isset($outByMonthSum[$month])){
                        $outByMonthSum[$month] = 0;
                    }
                    $outByMonthSum[$month] += $money;
                    ?>
                    <TD class="number"><?php echo number_format($money, 2, ".", "");?></TD>
                    <?php
                }
                ?>
            </TR>
            <?php
        }
        ?>

        <TR  id="content">
            <TD></TD>
            <TD class="to_left">支出小计</TD>
            <?php
            $outSum = 0;
            foreach($outByMonthSum as $month=>$money){
                if(!isset($allByMonthSum[$month])){
                    $allByMonthSum[$month] = 0;
                }
                $allByMonthSum[$month] -= $money;
                $outSum += $money;
                ?>
                <TD class="number"><?php echo number_format($money, 2, ".", "");?></TD>
                <?php
            }
            ?>
            <TD class="number" title="<?php echo number_format($outSum, 2, ".", "");?>"><?php echo number_format($outSum, 2, ".", "");?></TD>
        </TR>




        <TR style="FONT-WEIGHT: BOLD; COLOR: #000066;background-color:#fffff0;">
            <TD></TD>
            <TD class="char" style="padding-left:5px;">总合计</TD>
            <?php
            $allSum = 0;
            foreach($allByMonthSum as $money){
                $allSum += $money;
                ?>
                <TD class="number"><?php echo number_format($money, 2, ".", "");?></TD>
                <?php
            }
            ?>
            <TD class="number" title="<?php echo number_format($allSum, 2, ".", "");?>"><?php echo number_format($allSum, 2, ".", "");?></TD>
        </TR>

        <tr><td colspan="15"  class="to_left">(注：按最新汇率,以人民币计)</td></tr>
    </table>

    <?php
}else if($method=="pole") {
    // 所选年每月的收入与支出
    $year = isset($_POST["year"])?$_POST["year"]:null;
    $tagIds = isset($_POST["tagId"])?trim($_POST["tagId"], ","):"";
    $categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:null;

    $categories = array();
    $inList = array();
    $outList = array();
    for($month = 1;$month<=12;$month++){
        $nextMonthYear = $year;
        $nextMonth = $month+1;
        if($nextMonth>12){
            $nextMonth = 1;
            $nextMonthYear++;
        }
        $startTime = strtotime($year."-".$month."-01 00:00:00");
        $endTime = strtotime($nextMonthYear."-".$nextMonth."-01 00:00:00")-1;

        $thisDayIn = $flowClass->getCountByTimeRange($userId,
            tally_flow::$INOUT_INCOMME, $startTime, $endTime, $tagIds, $categoryId);
        $thisDayOut = $flowClass->getCountByTimeRange($userId,
            tally_flow::$INOUT_EXPENSE, $startTime, $endTime, $tagIds, $categoryId);
        $categories[] = date("m月", $startTime);
        $inList[] = abs($thisDayIn);
        $outList[] = abs($thisDayOut);
    }

    echo json_encode(array(
        "categories" => $categories,
        "series" => array(
            array(
                "name" => '收入',
                "data" => $inList

            ), array(
                "name" => '支出',
                "data" => $outList

            )
        ),
    ));
}else{
    $flowClass = new tally_flow();
    $flowList = $flowClass->findFlowByUserId($userId, 0, 0, "", "",
        "", "", 0, 0, "tradeTimeAsc", 0, 1);
    $firstTradeYear = date("Y", strtotime($flowList[0]["tradeTime"]));
    $currentYear = date("Y", time());
    ?>
    <!-- begin:导航条 -->
    <div class="titlebar">
        <h3>月度分类收支表</h3>
    </div>

    <div class="tab_wrapper">
        <div class="body">
            <!-- begin:搜索区 -->
            <form id="schForm" name="schForm" method="post">
                <div class="searchbar" id="searchbar" style="line-height: 150%; display: block;">
                    <div class="base_condition">
                        <table>
                            <tr>
                                <TD><label for="year">统计年度:</label>
                                    <SELECT name="year" id="year">

                                        <?php
                                        for($i=$firstTradeYear;$i<=$currentYear;$i++) {
                                            ?>
                                            <option value="<?php echo $i;?>" <?php echo $i==$currentYear?"selected":""?>>
                                                <?php echo $i;?>
                                            </option>
                                            <?php
                                        }
                                        ?>

                                    </SELECT>
                                </TD>
                                <td><input id="searchBtn" class="button" type="button" value="查询" onclick="MonthCategoryReport.search();Report.clearData();Report.searchChart();" /></td>
                            </tr>
                        </table>
                    </div>

                    <TABLE class="complex_condition">
                        <TR>
                            <td>
                                <label for="tags">
                                    标签:
                                </label>
                                <input type="text" class="text rd" id="tags" name="tags"
                                       readonly="readonly" onClick="CM.clearValue(this);">
                                <input type="hidden" value="" name="tagId" id="tagId"/>
                                <a title="选择项目" href="#"
                                   onclick="TagPicker.open(this,'1');return false;"><img src="/images/icons/select.gif" align="absmiddle" />
                                </a>
                            </td>

                            <td>
                                <label for="categoryName">
                                    收支类别:
                                </label>
                                <input type="hidden" id="categoryId" name="categoryId" />
                                <input type="text" class="text rd" id="categoryName" name="categoryName" readonly="readonly" onClick="CM.clearValue(this);" >
                                <a title="收支类别" href="#"
                                   onclick="CategoryPicker.open(this,'');return false;"><img
                                        src="/images/icons/select.gif" align="absmiddle" />
                                </a>
                            </td>

                            <TD>
                                <INPUT TYPE="radio" NAME="flag" value="1" checked hidden>
                            </TD>

                        </tr>


                    </TABLE>

                </div>
            </form>
            <!-- end:搜索区 -->

            <div class="report_bar">
                <span>数据列表</span>
            </div>

            <div id="dataView" class="data">
                <div align="center">
                    <img align="absmiddle" src='/images/common/loading1.gif'>
                </div>
            </div>


            <div class="report_bar">
                <span>数据图表</span>
            </div>

            <div id="chartView" class="chartView">
                <div  align="center"><img align="absmiddle" src='/images/common/loading1.gif'></div>
            </div>
            <!-- end:---------主数据区 -->
        </div>
    </div>
    <?php
}
?>
